package cn.com.yiqi.wsc.portal.controller.banner;

import cn.com.yiqi.wsc.common.Result;
import cn.com.yiqi.wsc.common.ResultStatusEnum;
import cn.com.yiqi.wsc.common.ResultUtil;
import cn.com.yiqi.wsc.dao.app_banner.criteria.AppBannerCriteria;
import cn.com.yiqi.wsc.entity.shop.AppBanner;
import cn.com.yiqi.wsc.enumeration.BannerKind;
import cn.com.yiqi.wsc.portal.controller.BaseController;
import cn.com.yiqi.wsc.service.app_banner.PortalAppBannerService;
import cn.com.yiqi.wsc.service.user.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by 苟宇恒 on 2017/2/23.
 * Banner管理
 */
@Controller
@RequestMapping("/banner")
public class BannerController extends BaseController {


    @Autowired
    private PortalAppBannerService portalAppBannerService;
    @Autowired
    private UserService userService;

    @RequestMapping("/index")
    public ModelAndView index() {
        ModelAndView mv = new ModelAndView("banner/index");
        this.getUserData(mv);
        return mv;
    }

    @RequestMapping("/list")
    @ResponseBody
    public Result pageList(@RequestBody AppBannerCriteria criteria) {
        Result result = new Result();

        result = portalAppBannerService.pageList(criteria);

        return result;
    }


    @RequestMapping("/insert")
    @ResponseBody
    public Result insert(@RequestBody AppBanner appBanner) {


        Result result = new Result();

        Result validation = validation(appBanner, result);
        if (validation != null) {
            return validation;
        }
        result = portalAppBannerService.insert(appBanner);

        return result;
    }

    private Result validation(@RequestBody AppBanner appBanner, Result result) {
        Integer kind = appBanner.getKind();
        if (kind == null) {

            return ResultUtil.paramError(result, "类型不能为空！");
        }
        if (kind != BannerKind.OTHER.getValue() && kind != BannerKind.NO_PIC.getValue()) {
            if (appBanner.getKindId() == null) {

                return ResultUtil.paramError(result, "关联内容不能为空！");
            }
        }

        if (kind == BannerKind.OTHER.getValue()) {
            if (StringUtils.isEmpty(appBanner.getForwardUrl())) {

                return ResultUtil.paramError(result, "目标地址不能为空！");
            }
        }
        if (StringUtils.isEmpty(appBanner.getBannerUrl())) {

            return ResultUtil.paramError(result, "图片不能为空！");
        }
        return null;
    }

    @RequestMapping("/update")
    @ResponseBody
    public Result update(@RequestBody AppBanner appBanner) {
        Result result = new Result();

        if (appBanner.getId() == null) {
            result.setStatus(ResultStatusEnum.参数错误.getCode());
            result.setErrMsg("参数错误");
            return result;
        }
        appBanner.setInsertUserId(userService.getCurrentUserId());
        result = portalAppBannerService.update(appBanner);

        return result;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Result delete(@RequestBody AppBanner appBanner) {
        Result result = new Result();

        if (appBanner.getId() == null) {
            result.setStatus(ResultStatusEnum.参数错误.getCode());
            result.setErrMsg("参数错误");
            return result;
        }
        result = portalAppBannerService.delete(appBanner);

        return result;
    }


    @RequestMapping("/detail")
    @ResponseBody
    public Result detail(@RequestBody AppBannerCriteria criteria) {
        Result result = new Result();

        if (criteria.getId() == null) {
            result.setStatus(ResultStatusEnum.参数错误.getCode());
            result.setErrMsg("参数错误");
            return result;
        }
        result = portalAppBannerService.detail(criteria);

        return result;
    }

    @RequestMapping("/enable")
    @ResponseBody
    public Result enable(@RequestBody AppBanner appBanner) {
        Result result = new Result();

        if (appBanner.getId() == null) {
            result.setStatus(ResultStatusEnum.参数错误.getCode());
            result.setErrMsg("参数错误");
            return result;
        }
        result = portalAppBannerService.enable(appBanner);

        return result;
    }


    @RequestMapping("/disable")
    @ResponseBody
    public Result disable(@RequestBody AppBanner appBanner) {
        Result result = new Result();

        if (appBanner.getId() == null) {
            result.setStatus(ResultStatusEnum.参数错误.getCode());
            result.setErrMsg("参数错误");
            return result;
        }


        result = portalAppBannerService.disable(appBanner);

        return result;
    }
}
